package com.ndp.fb.rdb.mongo.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.WriteConcern;
import com.ndp.fb.constant.MongoConstants;
import com.ndp.fb.mongo.api.StrategyRuleSpecificMongoService;
import com.ndp.fb.mongo.model.autocreatead.robot.StrategyRuleSpecific;
@Service
public class StrategyRuleSpecificMongoServiceImpl implements StrategyRuleSpecificMongoService {
	private static Logger logger = LoggerFactory.getLogger(StrategyRuleSpecificMongoServiceImpl.class);

	@Autowired
	private DB adResourceMongoDBClientPrimary;

	@Override
	public void save(StrategyRuleSpecific strategyRuleSpecific) {
		try {
			DBObject dbObject = (DBObject) com.mongodb.util.JSON.parse(JSONObject.toJSONString(strategyRuleSpecific));
			DBCollection dbCollection = adResourceMongoDBClientPrimary
					.getCollection(MongoConstants.TABLE_NAME_STRATEGYRULESPECIFIC);
			dbCollection.update(new BasicDBObject("id", dbObject.get("id")),dbObject,true,false,WriteConcern.JOURNALED);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}

	}

	@Override
	public StrategyRuleSpecific getById(String id) {
		try {
			DBCollection dbCollection = adResourceMongoDBClientPrimary
					.getCollection(MongoConstants.TABLE_NAME_STRATEGYRULESPECIFIC);
			DBObject dbObject = dbCollection.findOne(new BasicDBObject("id", id));
			return JSONObject.parseObject(JSONObject.toJSONString(dbObject), StrategyRuleSpecific.class);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}

	@Override
	public StrategyRuleSpecific queryByWeFeedIdAndStrategyRuleId(Long weFeedId, String strategyRuleId) {
		try {
			DBCollection dbCollection = adResourceMongoDBClientPrimary
					.getCollection(MongoConstants.TABLE_NAME_STRATEGYRULESPECIFIC);
			DBObject dbObject = dbCollection.findOne(new BasicDBObject("weFeedId", weFeedId).append("strategRuleId", strategyRuleId));
			return JSONObject.parseObject(JSONObject.toJSONString(dbObject), StrategyRuleSpecific.class);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}

}
